﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using OnlineMusic.lrc;
using System.Threading;

namespace OnlineMusic
{
    public partial class LrcForm : Form
    {
        private List<LrcText> lrcs = new List<LrcText>();
        private int lrcindex = -1;
        private int nexttime = 999999;
        private int lrcflag = 1;
        private BackgroundLabel[] lrcshow = new BackgroundLabel[2];
        private DateTime starttime;

        public LrcForm()
        {
            InitializeComponent();
            lrcshow[0] = ltext1;
            lrcshow[1] = ltext2;
            ltext1.DrawString("测试内容");
        }

        private void Lrc_Load(object sender, EventArgs e)
        {
            this.TransparencyKey = BackColor;
            Rectangle rect = SystemInformation.VirtualScreen;
            this.Width = rect.Width - 200;
            this.Location = new Point(100, SystemInformation.WorkingArea.Height - this.Height);
        }

        private void Reset()
        {
            lrcs.Clear();
            lrcindex = -1;
            nexttime = 999999;
            lrcflag = 1;
            this.starttime = DateTime.Now;
            this.timer1.Enabled = false;
            lrcshow[0].Text = "";
            lrcshow[1].Text = "";
        }

        public void ShowLrc(string name, bool anti)
        {
            Reset();
            string title = "", singer = "";
            int flag = name.LastIndexOf('-');
            if (flag != -1)
            {
                if (anti)
                {
                    title = name.Substring(flag + 1).Trim();
                    singer = name.Substring(0, flag).Trim();
                }
                else
                {
                    title = name.Substring(0, flag).Trim();
                    singer = name.Substring(flag + 1).Trim();
                }
            }
            else
            {
                title = name;
            }

            ParameterizedThreadStart tStart = new ParameterizedThreadStart(LoadLrc);
            Thread thread = new Thread(tStart);
            thread.Start(new LrcParam(this, title, singer));
        }

        public static void LoadLrc(Object arg)
        {
            LrcParam lrcParam = (LrcParam)arg;

            LrcAdapter lrcAdapter = LrcFactory.instance();
            string lrcurl = lrcAdapter.FindLrc(lrcParam.GetTitle(), lrcParam.GetSinger());
            if (lrcurl == null)
            {
                return;
            }
            string lrcText = lrcAdapter.LoadLrc(lrcurl);
            StringReader reader = new StringReader(lrcText);
            LrcForm lrcForm = lrcParam.GetLrcForm();
            lrcForm.lrcs.Clear();
            lrcForm.lrcs = null;
            lrcForm.lrcs = lrcAdapter.ReadLrc(lrcText);
            lrcForm.LoadNextLrc();
            lrcForm.Invoke(new MethodInvoker(() =>
            {
                lrcForm.timer1.Enabled = true;
            }));
            // lrcForm.timer1.Start();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            double diff = DateTime.Now.Subtract(this.starttime).TotalMilliseconds;
            if (diff > this.nexttime)
            {
                LoadNextLrc();
                this.lrcshow[this.lrcflag].ForeColor = System.Drawing.Color.ForestGreen;
                this.lrcflag = (this.lrcflag + 1) % 2;
            }
        }

        private void LoadNextLrc()
        {
            CheckForIllegalCrossThreadCalls = false;
            this.lrcindex++;
            if (this.lrcindex < this.lrcs.Count)
            {
                this.nexttime = this.lrcs[this.lrcindex].Time;
                this.lrcshow[(this.lrcflag + 1) % 2].Text = this.lrcs[this.lrcindex].Text;
                this.lrcshow[(this.lrcflag + 1) % 2].ForeColor = System.Drawing.Color.White;
            }
            else
            {
                this.timer1.Enabled = false;
            }
        }

        private void ReadLrc(string lrc)
        {
        }

        private void Lrc_SizeChanged(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
        }

        class LrcParam
        {
            private LrcForm lrcForm;
            private string title;
            private string singer;

            public LrcParam(LrcForm lrcForm, string title, string singer)
            {
                this.lrcForm = lrcForm;
                this.title = title;
                this.singer = singer;
            }

            public LrcForm GetLrcForm()
            {
                return lrcForm;
            }

            public string GetTitle()
            {
                return title;
            }

            public string GetSinger()
            {
                return singer;
            }
        }
    }
}
